<?php
 
	/*
	 * Created by Nicky
	 *
	*/
	 /* permission name 
	 karPenilaian_Student
	 karPenilaian_Student_add
	 karPenilaian_Student_edit
	 */
	class Isi_nilaiController extends CController{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Karyawan";
		public $subMenu = "";
		public $title = "";		
		public $loginUser;		
		public function init() {
			parent::init();
			if (!LoginUser::isLogin("karyawan")){
				LoginUser::redirectToLoginPage("karyawan");
			}else{
				$this->loginUser = LoginUser::getUser();
			}
						
		}
		
		public function actionIndex(){
			LoginUser::getPermission("karyawan_penilaian_student");
			$this->subMenu = "Penilaian";
			$this->layout ="//layouts/ly_blank";
			$karyawan = $this->loginUser ->karyawan;
			$department_id = $karyawan->department_id;	
			$department_name = $karyawan->department->name;					
			$karyawan_name = $karyawan->name;	
			$karyawan_id=$karyawan->id;				
						
 			$module= $this->loginUser ->module;
						
  			if ($module=="akademik")
			{
			$departments = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE    group by department_id order by id");
			} else 
			{
			$departments = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId  group by department_id order by id",array(":departmentId"=>$department_id));
			}

			if ($departments == null){
				$department_id == "";
			}else{	
				$department_id = Yii::app()->request->getParam("department_id", $departments[0]->department_id);
			}
			
	 		$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");	
	 		$semester_id = Yii::app()->request->getParam("semester_id","");	
	 		$tingkatan_id = Yii::app()->request->getParam("tingkatan_id","");				
	 		$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");			
	 		$pelajaran_id = Yii::app()->request->getParam("pelajaran_id","");		
	 		$walikelas_id = Yii::app()->request->getParam("walikelas_id","");	
			

			$tahunAjarans = VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId   group by tahun_ajaran_id order by tahun_ajaran_id desc  ",array(":departmentId"=>$department_id));			
			if ($tahunAjarans == null){
				$tahun_ajaran_id == "";
			}else{
				$tahun_ajaran_id = $tahun_ajaran_id == "" ? $tahunAjarans[0]->tahun_ajaran_id : $tahun_ajaran_id;
			}			
			$semesters = MSemester::model()->findAll("tahun_ajaran_id=:tahun_ajaran_id and is_active = TRUE and department_id='".$department_id."' order by id",array(":tahun_ajaran_id"=>$tahun_ajaran_id));
			//$semester_id = "";
			if ($semesters == null){
				$semester_id == "";
			}else{
				$semester_id = $semester_id == "" ? $semesters[0]->id : $semester_id;
			}
            $tingkatans =  VJadwalPelajaranKelasTahunAjaran::model()->findAll("is_active = TRUE and department_id=:departmentId and tahun_ajaran_id=:tahun_ajaran_id  group by tingkatan_id order by id",array(":departmentId"=>$department_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
//            $tingkatan_id = $tingkatans[0]->id ; 
			if ($tingkatans == null){
				$tingkatan_id == "";
			}else{
				$tingkatan_id = $tingkatan_id == "" ? $tingkatans[0]->tingkatan_id : $tingkatan_id;
			}
			
			$mcon= Yii::app()->db;						
			$kelass = VJadwalPelajaranKelasTahunAjaran::model()->findAll("tingkatan_id=:tingkatan_id and department_id=:departmentId and tahun_ajaran_id=:tahun_ajaran_id  and is_active = TRUE group by kelas_id,tingkatan_id order by id",array(":tingkatan_id"=>$tingkatan_id,":departmentId"=>$department_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
 
			if ($kelass == null){
				$kelas_tahun_ajaran_id == "";
			}else{
				$kelas_tahun_ajaran_id = $kelas_tahun_ajaran_id == "" ? $kelass[0]["kelas_tahun_ajaran_id"] : $kelas_tahun_ajaran_id;
			}					
 
			 $pelajarans = VTaskPelajaranDetailSemester::model()->findAll("kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id and is_active = TRUE and semester_id=:semester_id group by pelajaran_id  order by id",array(":kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id,":semester_id"=>$semester_id));
			$pelajaran_id  = "";
			if ($pelajarans == null){
				$pelajaran_id == "";
			}else{
			
				$pelajaran_id = $pelajaran_id == "" ? $pelajarans[0]->pelajaran_id : $pelajaran_id;
			}
			
			$task_pelajarans = VTaskPelajaranDetailSemester::model()->findAll("kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id and pelajaran_id=:pelajaran_id and is_active = TRUE and semester_id=:semester_id  group by task_pelajaran_id order by id",array(":pelajaran_id"=>$pelajaran_id,":kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id,":semester_id"=>$semester_id));
			$task_pelajaran_id = "";
			if ($task_pelajarans == null){
				$task_pelajaran_id == "";
			}else{
				$task_pelajaran_id = $task_pelajaran_id == "" ? $task_pelajarans[0]->task_pelajaran_id : $task_pelajaran_id;
			}

		 $task_pelajaran_details = VTaskPelajaranDetailSemester::model()->findAll("task_pelajaran_id=:task_pelajaran_id and kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id and semester_id=:semester_id and is_active = TRUE   group by id  order by id",array(":task_pelajaran_id"=>$pelajaran_id,":kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id,":semester_id"=>$semester_id));
			$task_pelajaran_detail_id = "";
			if ($task_pelajaran_details == null){
				$task_pelajaran_detail_id == "";
			}else{
				$task_pelajaran_detail_id = $task_pelajaran_detail_id == "" ? $task_pelajaran_details[0]->id : $task_pelajaran_detail_id;
			}						
			
						
			$data=array(
					"department_id"=>$department_id,
					"departments" => $departments,						
					"karyawan_name"=>$karyawan_name,
					"karyawan_id"=>$karyawan_id,										
					"tahun_ajaran_id"=>$tahun_ajaran_id,
					"tingkatans"=>$tingkatans,							
					"tingkatan_id"=>$tingkatan_id,					
					"tahunAjarans"=>$tahunAjarans,					
					"semesters"=>$semesters,
					"semester_id" => $semester_id,
					"kelass"=>$kelass,
					"kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id,
					"department_name"=>$department_name,
					"pelajarans"=>$pelajarans,
					"pelajaran_id"=>$pelajaran_id,
					"task_pelajarans"=>$task_pelajarans,
					"task_pelajaran_id"=>$task_pelajaran_id,
					"task_pelajaran_details"=>$task_pelajaran_details,
					"task_pelajaran_detail_id"=>$task_pelajaran_detail_id,														
					);			
			$this->render('index',$data);
		}
 	
		public function actiondetailList(){		
			$onlyActive = Yii::app()->request->getParam("activeOnly",false);
			$department_id = Yii::app()->request->getParam("department_id","");
			$karyawan_id = Yii::app()->request->getParam("karyawan_id","");
			$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");
			$semester_id = Yii::app()->request->getParam("semester_id","");
			$pelajaran_id = Yii::app()->request->getParam("pelajaran_id","");
			$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");
			$task_pelajaran_id = Yii::app()->request->getParam("task_pelajaran_id","");	
			$task_pelajaran_detail_id = Yii::app()->request->getParam("task_pelajaran_detail_id","");						
			$mcon= Yii::app()->db;
			if ($task_pelajaran_id=="") {$task_pelajaran_id=0;}
			if ($task_pelajaran_detail_id=="") {$task_pelajaran_detail_id=0;}
			if ($semester_id=="") {$semester_id=0;}		
			if ($pelajaran_id=="") {$pelajaran_id=0;}									
			if ($kelas_tahun_ajaran_id=="") {$kelas_tahun_ajaran_id=0;}					
			//if($task_pelajaran_id==0){$department_id="00";} else {$department_id=$department_id;}
	 
	$sql1='SELECT B.id as id,VKTA.id as kelas_tahun_ajaran_id  ,VKTA.department_id,VKTA.tahun_ajaran_id,sid, B.name,
case when mpt.id is null then "0" else mpt.id end as stat_id,task_date,case when mpt.id is null then "" else mpt.nilai end  as nilai, mpt.notes as notes,
case when mpt.id is null then 1 else mpt.karyawan_id end  as karyawan_id, 
case when mpt.id is null then 1 else mpt.pelajaran_id end  as pelajaran_id, 
case when mpt.id is null then 1 else mpt.task_pelajaran_id end  as task_pelajaran_id,   
case when mpt.id is null then 1 else mpt.task_pelajaran_detail_id end  as task_pelajaran_detail_id,
semester_id , mpt.department_id
FROM  v_kelas_tahun_ajaran VKTA 
left join  `m_student_class` A on A.kelas_tahun_ajaran_id=VKTA.id 
left join m_student B on A.student_id=B.id
LEFT OUTER JOIN 
(select P.id as id, P.karyawan_id, VTPDS.id as task_pelajaran_detail_id,P.nilai,P.notes,P.student_id, VTPDS.task_pelajaran_id as task_pelajaran_id,VTPDS.task_date, VTPDS.pelajaran_id,VTPDS.department_id,VTPDS.semester_id, VTPDS.kelas_tahun_ajaran_id
from `m_penilaian` P 
left join v_task_pelajaran_detail_semester VTPDS on P.task_pelajaran_detail_id = VTPDS.id where P.karyawan_id='.$karyawan_id.' and  VTPDS.pelajaran_id='.$pelajaran_id.'    and VTPDS.task_pelajaran_id ='.$task_pelajaran_id.' and VTPDS.kelas_tahun_ajaran_id='.$kelas_tahun_ajaran_id.'    and P.task_pelajaran_detail_id ='.$task_pelajaran_detail_id.' and  VTPDS.semester_id='.$semester_id.' order by student_id,semester_id asc   ) mpt on (mpt.student_id=B.id  and VKTA.id=mpt.kelas_tahun_ajaran_id and B.department_id=mpt.department_id)
where VKTA.is_active=True and        VKTA.department_id="'.$department_id.'" and VKTA.tahun_ajaran_id ='.$tahun_ajaran_id.'  and   A.kelas_tahun_ajaran_id='.$kelas_tahun_ajaran_id.' 
 

 ';
 
 
			$mPenilaianCommand=$mcon->createCommand($sql1);
			$mPenilaianCommand=$mPenilaianCommand->queryAll();				
 	
		 	$this->result["actionStatus"] =0;
			$this->result["actionMessage"] =$sql1;
			$this->result["actionError"] =array();
			$this->result["model"] =   $mPenilaianCommand;			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
			 
			 
		}
	 
 
		public function actionedit(){		
			$op = Yii::app()->request->getParam("op","add");
			$status = 1;//Yii::app()->request->getParam("is_active",1) === "on"?1:0 ;			
			if ($op == "add"){
				LoginUser::getPermission("karyawan_penilaian_student_add");
				//print_r ($_POST);
				//echo "DATA SUDAH DI ADD........";
				$penilaian = new MPenilaian;
				$penilaian->input_datetime=date("Y-m-d H:i:s");				
				$penilaian->student_id= Yii::app()->request->getParam("student_id","");	
				$penilaian->karyawan_id= Yii::app()->request->getParam("karyawan_id","");	
//				$penilaian->tahun_ajaran_id= Yii::app()->request->getParam("tahun_ajaran_id","");	
//				$penilaian->kelas_tahun_ajaran_id= Yii::app()->request->getParam("kelas_tahun_ajaran_id","");													
				$penilaian->task_pelajaran_detail_id= Yii::app()->request->getParam("task_pelajaran_detail_id","");		
				$penilaian->nilai=Yii::app()->request->getParam("nilai","0");
				$penilaian->notes=Yii::app()->request->getParam("notes","-");

				$penilaian->is_active=$status;
//				print_r($penilaian);
				if ($penilaian->save()){
					echo "<script>
						alert('".Yii::t("dialog","Data sudah di tambahkan...!").   "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($penilaian->getErrors());
				}				
			}else if ($op == "edit"){
				LoginUser::getPermission("karyawan_penilaian_student_edit");
				//print_r ($_POST);
				$id = Yii::app()->request->getParam("id","");				
				$penilaian = MPenilaian::model()->find("id=:id",array(":id"=>$id));
				$penilaian->input_datetime=date("Y-m-d H:i:s");								
				$penilaian->student_id= Yii::app()->request->getParam("student_id","");	
				$penilaian->karyawan_id= Yii::app()->request->getParam("karyawan_id","");	
//				$penilaian->tahun_ajaran_id= Yii::app()->request->getParam("tahun_ajaran_id","");	
//				$penilaian->kelas_tahun_ajaran_id= Yii::app()->request->getParam("kelas_tahun_ajaran_id","");													
				$penilaian->task_pelajaran_detail_id= Yii::app()->request->getParam("task_pelajaran_detail_id","");		
				$penilaian->nilai=Yii::app()->request->getParam("nilai","0");
				$penilaian->notes=Yii::app()->request->getParam("notes","");					
				$penilaian->is_active=$status;
				if ($penilaian->save()){
					echo "<script>
						alert('".Yii::t("dialog","Perubahan data sudah di simpan...!"). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($penilaian->getErrors());
				}				
			} 
			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
			
		}
		
		/*
		public function actionform(){
			$op = Yii::app()->request->getParam("op","add");
			$callback = Yii::app()->request->getParam("callback","");
			$department_id = Yii::app()->request->getParam("department_id","0");
			
			$data = array("op"=>"add","callback"=>"","id"=>0,"department_id" =>$department_id,"name"=>"","singkatan"=>"","keterangan"=>"","is_active"=>"1");
			
			if ($op == "add"){
				LoginUser::getPermission("akademik_gdp_jadwalpelajaran_add");
				$data["op"] = "add";
				$data["callback"] = $callback;				
			}else if($op =="edit"){				
				LoginUser::getPermission("akademik_gdp_jadwalpelajaran_edit");			
				$data["op"] = "edit";
				$data["callback"] = $callback;				
				$id = Yii::app()->request->getParam("id","0");				
				$data["id"] = $id;
				
//				$rsJadwalPelajaran = MJadwalPelajaran::model()->findAll(array("condition"=>"id = $id","order"=>"id"));
				$mcon= Yii::app()->db;
				$sql1='select A.id as id ,A.tahun_ajaran_id,A.kelas_id,A.karyawan_id,A.pelajaran_id,A.status,A.hari,A.pelajaran_ke,A.pelajaran_sampai,A.is_active,A.keterangan,B.tingkatan_id as tingkatan_id from  m_jadwal_pelajaran A left join m_kelas B on A.kelas_id=B.id where A.id='.$id.' order by A.id';
				$mJadwalPelajaran=$mcon->createCommand($sql1);		
				$rsDep=$mJadwalPelajaran->queryAll();	
				$json = CJSON::encode($rsDep);
				$arrdata = json_decode($json,true);
				if (count($arrdata) > 0){
					for ($x=0; $x<count($arrdata); $x++)
					{				
						$data["tahun_ajaran_id"] =$arrdata[$x]['tahun_ajaran_id'];	
						$data["kelas_id"] = $arrdata[$x]['kelas_id'];									
						$data["karyawan_id"] = $arrdata[$x]['karyawan_id'];	
						$data["pelajaran_id"] =$arrdata[$x]['pelajaran_id'];	
						$data["status"] = $arrdata[$x]['status'];			
						$data["hari"] = $arrdata[$x]['hari'];	
						$data["pelajaran_ke"] = $arrdata[$x]['pelajaran_ke'];	
						$data["pelajaran_sampai"] = $arrdata[$x]['pelajaran_sampai'];											
						$data["tingkatan_id"] = $arrdata[$x]['tingkatan_id'];			
						$data["keterangan"] = $arrdata[$x]['keterangan'];																																				
						$data["is_active"] =$arrdata[$x]['is_active'];	
					}
				}				
			}			
			$this->layout ="//layouts/ly_blank";
			$this->render("form",$data);
		}
		*/		
				
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
	public function actionPopupWindow()
	{
		$this->layout = "//layouts/ly_blank";
		$this->render('popupWindow');
	}		
		
	}
?>